﻿using SkiaSharp;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Mercury.Core.Extensions
{
    public static class SecurityEncDecryptExtensions
    {

        private static byte[] Keys = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
        /// <summary> 
        /// DES加密字符串 
        /// </summary> 
        /// <param name="encryptString">待加密的字符串</param> 
        /// <param name="encryptKey">加密密钥,要求为16位</param> 
        /// <returns>加密成功返回加密后的字符串，失败返回源串</returns> 

        public static string EncryptDES(this string encryptString, string encryptKey)
        {
            try
            {
                byte[] key = Encoding.UTF8.GetBytes(encryptKey);
                byte[] iv = Keys;
                byte[] encrypted;
                using (Aes aes = Aes.Create())
                {
                    aes.Key = key;
                    aes.IV = iv;
                    ICryptoTransform transform = aes.CreateEncryptor();
                    using (MemoryStream stream = new MemoryStream())
                    {
                        using (CryptoStream crypto = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                        {
                            using (StreamWriter writer = new StreamWriter(crypto))
                            {
                                writer.Write(encryptString);
                            }
                            encrypted = stream.ToArray();
                        }
                    }
                }
                return Convert.ToBase64String(encrypted).Replace('+', '_').Replace('/', '~');
            }
            catch (Exception ex)
            {
                throw new Exception("密码加密异常" + ex.Message);
            }

        }

        /// <summary> 
        /// DES解密字符串 
        /// </summary> 
        /// <param name="decryptString">待解密的字符串</param> 
        /// <param name="decryptKey">解密密钥,和加密密钥相同</param> 
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns> 

        public static string DecryptDES(this string decryptString, string decryptKey)
        {
            byte[] data = Convert.FromBase64String(decryptString.Replace('_', '+').Replace('~', '/'));
            byte[] key = Encoding.UTF8.GetBytes(decryptKey);
            byte[] iv = Keys;
            string original = null;

            using (Aes aes = Aes.Create())
            {
                aes.Key = key;
                aes.IV = iv;
                ICryptoTransform transform = aes.CreateDecryptor();
                using (MemoryStream stream = new MemoryStream(data))
                {
                    using (CryptoStream crypto = new CryptoStream(stream, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader reader = new StreamReader(crypto))
                        {
                            original = reader.ReadToEnd();
                        }
                    }
                }
            }
            return original;
        }
        public static bool TryDecryptDES(this string decryptString, string decryptKey, out string result)
        {
            result = "";
            try
            {
                result = DecryptDES(decryptString, decryptKey);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="originString"></param>
        /// <returns></returns>
        public static string EncryptMD5(this string originString) 
        {
            var md5 = MD5.Create();
            var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(originString));
            var sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2"));
            }
            //所有字符转为大写
            return sb.ToString().ToLower();
        }
    }
}
